package com.showsu.editor.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblXhsEditorMediaRecordMapper;
import com.showsu.editor.entity.po.TblXhsDewaterRecord;
import com.showsu.editor.entity.po.TblXhsEditorMediaRecord;
import com.showsu.editor.entity.vo.request.RequestVO;
import com.showsu.editor.entity.vo.response.XhsLiveVO;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.service.IXhsEditorMediaRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * xhs编辑器记录 服务类实现
 *
 * @author jph
 *  * @since 2024/9/19
 */
@Service
@Slf4j
public class XhsEditorMediaRecordServiceImpl extends ServiceImpl<TblXhsEditorMediaRecordMapper, TblXhsEditorMediaRecord> implements IXhsEditorMediaRecordService {

    @Autowired
    private TblXhsEditorMediaRecordMapper mapper;


    @Override
    public int queryCountOfUser(Long userId) {
        if (userId == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsEditorMediaRecord>().lambda()
                .eq(TblXhsEditorMediaRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .eq(TblXhsEditorMediaRecord::getUserId, userId));
    }

    @Override
    public int queryCountOfUserWithDate(Long userId, Date startDate, Date endDate) {
        if (userId == null || startDate == null || endDate == null) {
            return 0;
        }
        return mapper.selectCount(new QueryWrapper<TblXhsEditorMediaRecord>().lambda()
                .eq(TblXhsEditorMediaRecord::getUserId, userId)
                .ge(TblXhsEditorMediaRecord::getCreateTime, startDate)
                .le(TblXhsEditorMediaRecord::getCreateTime, endDate)
                .eq(TblXhsEditorMediaRecord::getFlag, RowFlagEnum.UnDelete.getCode()));
    }

    @Override
    public int saveRecord(RequestVO clientRequest, Long userId,Integer type, String url) {
        TblXhsEditorMediaRecord record = new TblXhsEditorMediaRecord();
        record.setId(IdUtil.objectId());
        record.setType(type);
        record.setMediaUrl(url);
        record.setClientBrowser(clientRequest.getClientBrowser());
        record.setClientIp(clientRequest.getClientIp());
        record.setClientOrigin(clientRequest.getClientOrigin());
        record.setClientOs(clientRequest.getClientOs());
        record.setUserId(userId);
        int rows = mapper.insert(record);
        return rows;
    }

    @Override
    public int saveRecord(RequestVO clientRequest, Long userId,Integer type, String url,String ocrText) {
        TblXhsEditorMediaRecord record = new TblXhsEditorMediaRecord();
        record.setId(IdUtil.objectId());
        record.setType(type);
        record.setMediaUrl(url);
        record.setOtherInfo(ocrText);
        record.setClientBrowser(clientRequest.getClientBrowser());
        record.setClientIp(clientRequest.getClientIp());
        record.setClientOrigin(clientRequest.getClientOrigin());
        record.setClientOs(clientRequest.getClientOs());
        record.setUserId(userId);
        int rows = mapper.insert(record);
        return rows;
    }

    @Override
    public int saveRecord(RequestVO clientRequest, Long userId,Integer type, String url,String ocrText,Integer status) {
        TblXhsEditorMediaRecord record = new TblXhsEditorMediaRecord();
        record.setId(IdUtil.objectId());
        record.setType(type);
        record.setMediaUrl(url);
        record.setOtherInfo(ocrText);
        record.setStatus(status);
        record.setClientBrowser(clientRequest.getClientBrowser());
        record.setClientIp(clientRequest.getClientIp());
        record.setClientOrigin(clientRequest.getClientOrigin());
        record.setClientOs(clientRequest.getClientOs());
        record.setUserId(userId);
        int rows = mapper.insert(record);
        return rows;
    }
}
